#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <cstdlib>
#include <sys/wait.h>

void WaitAll(int num)
{
    while(true)
    {
        int n=waitpid(-1, nullptr, WNOHANG);
        if(n ==0)
        {
            break;
        }
        else if(n<0)
        {
            std::cout<<"waitpid error"<<std::endl;
            break;
        }
    }
    std::cout << "father get a sianal:" << num << std::endl;
}

void Say(int num)
{
    std::cout << "father get a sianal:" << num << std::endl;
}
int main()
{
    signal(SIGCHLD, Say);
    pid_t id = fork();
    if (id == 0)
    {
        std::cout << "I am child,exit " << std::endl;
        sleep(3);
        exit(3);
    }
    waitpid(id, nullptr, 0);
    std::cout << "I am fater,exit" << std::endl;
    return 0;
}

/*int flag=0;

void handler(int signu)
{
    std::cout<<"更改全局变量"<<flag<<"->1"<<std::endl;
    flag=1;
}
int main()
{
    signal(2,handler);
    while(!flag);
    std::cout<<"process quit normal"<<std::endl;
    return 0;
}

/*void handler(int signum)
{
    std::cout<<"hello signal:"<<signum<<std::endl;
    while(true)
    {
        sigset_t pending;
        sigpending(&pending);
        for(int i=31;i>=1;i--)
        {
            if(sigismember(&pending,i))
                std::cout<<"1";
            else
                std::cout<<"0";
        }
        std::cout<<std::endl;
        sleep(1);
    }
    exit(0);
}

int main()
{
    struct  sigaction act,oact;
    act.sa_handler=handler;
    sigemptyset(&act.sa_mask);
    sigaddset(&act.sa_mask,3);
    sigaddset(&act.sa_mask,4);
    act.sa_flags=0;

    sigaction(SIGINT,&act,&oact);//对2号信号进行捕捉
    while(true)
    {
        std::cout<<"hello world: "<<getpid()<<std::endl;
        sleep(1);
    }
    return 0;
}*/